package com.joyintech.util;

import com.auth0.jwt.interfaces.Claim;
import com.sccba.cipher.Decipher;
import com.sccba.cipher.sm.request.RequestDataDecryptCipher;
import com.sccba.cipher.sm.response.ResponseDataEncryptCipher;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class Verify {

    /**
     * 解密token方法
     * @param token
     * @return
     */
    public static String verifyCbankWeb3Token(String token) {
         try{
            Map<String, Claim> result = Decipher.verifyCorporBankToken(token);
            for(String key : result.keySet()){
                log.info(key + "<=>" + result.get(key).asString());
            }
            return result.get("cstNo").asString();

        }catch (Exception e){
//            System.out.println("异常信息12344" + e.getMessage());
//            e.printStackTrace();
        }
        return "";
    }

    // 报文解密-手机银行
    public static String decMessage(String decData) throws Exception {
        try {
            String message = RequestDataDecryptCipher.decryptRequestAndvalidateHmac(decData);
//            log.info("解密后->{}", message);
            return message;
        }catch (Exception e){
//            log.error("解密异常",e);
//            log.error("解密异常返回报文->",decData);
            return decData;
        }
    }

    /**
     * 加密
     */
    // 报文加密-手机银行
    // 前提：先调解密，再调加密，且在一个main方法里完成
    public static String encMessage(String message) {
        try {
            String encData = ResponseDataEncryptCipher.encrypt(message);
            return encData;

        }catch (Exception e){
//            log.error("解密异常",e);
            return message;
        }
    }
}
